﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Workflow.Runtime;
using System.Workflow.ComponentModel;
using Openfree.Workflow.Common;
//using Openfree.Workflow.Workflow;
using Openfree.Framework.DataAccess;
using System.Text;

public partial class Controls_Activity_GoodItemOut : WorkflowUserControl
{

    public EventHandler OnSelect;
    private bool m_IsReadOnly;
    public bool IsReadOnly
    {
        get { return m_IsReadOnly; }
        set { m_IsReadOnly = value; }
    }
    //出库领料单表
    public string m_ReqTableName = "IC_Requisition";

    private string cmdItemsText = "SELECT T.*,C.ActiveCount,A.SUBJECT AS CategoryName FROM {0} T inner join IC_Goods C on C.GoodsId=T.GoodsId LEFT JOIN " + GoodsCategoryAccess.TABLE_NAME + " A ON A.NODEID=T.CategoryID where 1=1 ";

    private string cmdGoodsText = string.Format("SELECT T.*,A.SUBJECT AS CategoryName FROM {0} T LEFT JOIN " + GoodsCategoryAccess.TABLE_NAME + " A ON A.NODEID=T.CategoryID where 1=1 ", "IC_Goods");

 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 

        }
        OnSelect += new EventHandler(btnShowSelectItem);
        GoodItemSelect.OnGoodItemSelectOk += new GoodItemSelectEventHandler(btnSelectOk_Click); 
    }
    private void btnSelectOk_Click(object sender, EventArgs e)
    {
        BindGridViewItems();
    }
    private void btnShowSelectItem(object sender, EventArgs e)
    {
        GoodItemSelect.OnSelect(sender, e);
    }
    public int SelectItemsCount
    {
        get
        {
            return this.gridViewItems.Rows.Count;
        }
    }

    /// <summary>
    /// 返回string.Empty为正确，返回非空字符串为错误提示信息
    /// </summary>
    /// <returns></returns>
    public string PreProcessSave()
    {
        //获取当前库存信息
        PMSAccess.FillDataSet(this.Connection, SessionManager.DataSet, "select * from IC_Goods ", "IC_Goods");
        DataTable _gooddt = SessionManager.DataSet.Tables["IC_Goods"];
        DataRow[] _gooddrs = null;
        DataTable dt = SessionManager.DataSet.Tables[SessionManager.GetValue(PMSConst.m_TableItemName).ToString()];
        int n = -1;
        for (int i = 0; i < gridViewItems.Rows.Count; i++)
        {
            n++;
            if (dt.Rows[n].RowState == DataRowState.Deleted)
            {
                i--;
                continue;
            }
            int count = 0;
            try
            {
                count = Convert.ToInt32((gridViewItems.Rows[i].Cells[5].Controls[3] as TextBox).Text);
                //判断是否超过库存量:可用库存量和实际库存量
                _gooddrs = _gooddt.Select("GoodsID='" + dt.Rows[n]["GoodsID"].ToString() + "'");
                if (_gooddrs.Length > 0 && Convert.ToInt32(_gooddrs[0]["ActiveCount"])<count)
                {
                    return "物料数量已经超过当前活动库存量，请检查！";
                }
            }
            catch
            {
                return "数量请输入整数，其它值无效！";
            } 
                dt.Rows[n]["GoodsCount"] = count;
                dt.Rows[n]["Description"] = (gridViewItems.Rows[i].Cells[6].Controls[1] as TextBox).Text;
        }
        if (n == -1)
        {
            return "请选择具体物料项！";
        }
        return string.Empty;
    }
    public void InitData(string tablename, string key, string subkey, string subvalue)
    {
        SessionManager.SetValue(PMSConst.m_TableItemName, tablename);
        SessionManager.SetValue(PMSConst.m_Key, key);
        //现在的subkey subvalue是TaskID用完后需要转换为RequisitionID,与具体物料项关联
        SessionManager.SetValue(PMSConst.m_SubKey, subkey);
        SessionManager.SetValue(PMSConst.m_SubValue, subvalue);
        string cmdtext = string.Format(cmdItemsText, tablename);
        string str = "select * from IC_Requisition where 1=1 ";
        if (!string.IsNullOrEmpty(subvalue))
        {
            str += " AND " + subkey + " = '" + subvalue + "'";
            cmdtext += " AND RequisitionID in (select RequisitionID from IC_Requisition where " + subkey + " = '" + subvalue + "')";
        }
        else
        {
            str += " AND 1=2 ";
            cmdtext += " AND 1=2 ";
        }
        PMSAccess.FillDataSet(this.Connection, SessionManager.DataSet, str, m_ReqTableName);
        PMSAccess.FillDataSet(this.Connection, SessionManager.DataSet, cmdtext, tablename);
        if (SessionManager.DataSet.Tables[m_ReqTableName] != null && SessionManager.DataSet.Tables[m_ReqTableName].Rows.Count > 0)
        {
            DataRow dr = SessionManager.DataSet.Tables[m_ReqTableName].Rows[0];
            SessionManager.SetValue(PMSConst.m_SubKey, "RequisitionID");
            SessionManager.SetValue(PMSConst.m_SubValue, dr["RequisitionID"].ToString());
        }
        else
        {
            SessionManager.SetValue(PMSConst.m_SubKey, "RequisitionID");
            SessionManager.SetValue(PMSConst.m_SubValue, "");
        }
        BindGridViewItems();
    }
    public void InitDataOut(string tablename, string key, string subkey, string subvalue)
    {
        SessionManager.SetValue(PMSConst.m_TableItemName, tablename);
        SessionManager.SetValue(PMSConst.m_Key, key);
        //现在的subkey subvalue是TaskID用完后需要转换为RequisitionID,与具体物料项关联
        SessionManager.SetValue(PMSConst.m_SubKey, subkey);
        SessionManager.SetValue(PMSConst.m_SubValue, subvalue);
        string cmdtext = string.Format(cmdItemsText, tablename);
        if (!string.IsNullOrEmpty(subvalue))
        {
            cmdtext += " AND " + subkey + " = '" + subvalue + "'";
        }
        else
        {
            cmdtext += " AND 1=2 ";
        } 
        PMSAccess.FillDataSet(this.Connection, SessionManager.DataSet, cmdtext, tablename);
        SessionManager.SetValue(PMSConst.m_SubKey, "RequisitionID");
        SessionManager.SetValue(PMSConst.m_SubValue, "");
        BindGridViewItems();
    }
    #region 具体项操作
    protected void BindGridViewItems()
    {
        gridViewItems.ShowFooter = false;
        gridViewItems.DataSource = SessionManager.DataSet.Tables[SessionManager.GetValue(PMSConst.m_TableItemName).ToString()];
        gridViewItems.DataBind();
    }
    protected void gridViewItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((HyperLink)e.Row.Cells[0].Controls[0]).NavigateUrl = "~/PMSWeb/GoodDetail.aspx?GoodsID=" + ((HiddenField)e.Row.Cells[5].Controls[1]).Value;
            if (IsReadOnly)
            {
                (e.Row.Cells[5].Controls[3] as TextBox).ReadOnly = true;
                (e.Row.Cells[6].Controls[1] as TextBox).ReadOnly = true;
                (e.Row.Cells[8].Controls[0]).Visible = false; 
            }
            else
            {
                (e.Row.Cells[5].Controls[3] as TextBox).ReadOnly = false;
                (e.Row.Cells[6].Controls[1] as TextBox).ReadOnly = false;
                (e.Row.Cells[8].Controls[0]).Visible = true; 
            }
        }
    }

    protected void gridViewItems_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string tbn = SessionManager.GetValue(PMSConst.m_TableItemName).ToString();
        string ID = (gridViewItems.Rows[e.RowIndex].Cells[5].Controls[1] as HiddenField).Value;

        foreach (DataRow row in SessionManager.DataSet.Tables[tbn].Rows)
        {
            if (row.RowState != DataRowState.Deleted && ID.Equals(row["GoodsID"].ToString()))
            {
                row.Delete();
                BindGridViewItems();
                break;
            }
        }
    }
    #endregion 
    
}
